<?php

class LobbyView extends dmq\View
{
	public function buildLobby( $sectionData )
	{
		$this->markup( '        <ul id="lobby-nav">' . "\r\n" );
		$this->markup( '        <li id="' . strtolower( LobbyController::ALL_GAMES ) . '"' . ( ( $this->action() == LobbyController::ALL_GAMES ) ? ' class="active"' : '' ) . '><a href="index.php?c=lobby&amp;a=' . LobbyController::ALL_GAMES . '">All Games</a></li>' . "\r\n" );

		if ( $this->auth()->isLoggedIn() )
		{
			$this->markup( '        <li id="' . strtolower( LobbyController::MY_GAMES ) . '"' . ( ( $this->action() == LobbyController::MY_GAMES ) ? ' class="active"' : '' ) . '><a href="index.php?c=lobby&amp;a=' . LobbyController::MY_GAMES . '">My Games</a></li>' . "\r\n" );
			$this->markup( '        <li id="' . strtolower( LobbyController::CREATE_GAME ) . '"' . ( ( $this->action() == LobbyController::CREATE_GAME ) ? ' class="active"' : '' ) . '><a href="index.php?c=lobby&amp;a=' . LobbyController::CREATE_GAME . '">Create Game</a></li>' . "\r\n" );
			if ( $this->action() == LobbyController::GAME_DETAILS )
			{
				$this->markup( '        <li id="' . strtolower( LobbyController::GAME_DETAILS ) . '"' . ( ( $this->action() == LobbyController::GAME_DETAILS ) ? ' class="active"' : '' ) . '><a href="index.php?c=lobby&amp;a=' . LobbyController::GAME_DETAILS . '&amp;game=' . $sectionData['game']->id() . '">Game Details</a></li>' . "\r\n" );
			}
		}

		$this->markup( '        </ul>' . "\r\n" );

		$this->markup( '        <div id="lobby-container">' . "\r\n" );

		switch ( $this->action() )
		{
			case LobbyController::GAME_DETAILS:
				$this->buildGameDetails( $sectionData['game'] );
				break;

			case LobbyController::CREATE_GAME:
				$this->buildCreateGame( $sectionData['maps'] );
				break;

			case LobbyController::MY_GAMES:
				$this->buildGamesTable( $sectionData['games'] );
				break;

			case LobbyController::ALL_GAMES:
				$this->buildGamesTable( $sectionData['games'] );
				break;
		}

		$this->markup( '        </div>' . "\r\n" );
	}

	private function buildGameDetails( $game )
	{
		$this->markup( '          <div class="lobby-section">' . "\r\n" );

		if ( $game->id() === null )
		{
			$this->error( "There are no details for this game." );
		}
		else
		{
			$this->markup( '            <dl>' . "\r\n"  );
			$this->markup( '              <dt>Game ID</dt>' . "\r\n" );
			$this->markup( '              <dd>' . $game->id() . '</dd>' . "\r\n" );
			$this->markup( '              <dt>Map</dt>' . "\r\n"  );
			$this->markup( '              <dd>' . $game->map()->name(). '</dd>' . "\r\n"  );
			$this->markup( '              <dt>Status</dt>' . "\r\n"  );
			$this->markup( '              <dd>');

			if ( $game->finished() === 1 )
			{
				$this->markup( 'The game is over.');
			}
			elseif ( $game->started() === 1 )
			{
				$this->markup( 'The game is under progress (Turn: ' . $game->turn() . ').');
			}
			elseif ( $game->maxPlayers() > count( $game->players() ) )
			{
				$this->markup( 'Waiting for players to join.');
			}
			else
			{
				if ( $this->auth()->user()->id() === $game->creator()->id() )
				{
					$this->markup( 'Ready for you to start the game.');
				}
				else
				{
					$this->markup( 'Waiting for owner to start the game.');
				}
			}

			$this->markup( '</dd>' . "\r\n"  );
			$this->markup( '              <dt>Players (' . count( $game->players() ) . '/' . $game->maxPlayers() . ')</dt>' . "\r\n"  );
			$this->markup( '              <dd>' . "\r\n"  );
			$this->markup( '                <ul>' . "\r\n"  );


			$playerExists = false;
			foreach ( $game->players() as $player )
			{

				if ( $this->auth()->user()->id() === $player->user()->id() )
				{
					$playerExists = true;
				}

				$this->markup( '                  <li>' );

				if ( $player->user()->id() === $game->creator()->id() )
				{
					// The player is the game operator.
					$this->markup( 'OP: ' );
				}

				$this->markup( $player->user()->name() );
				
				if ( $game->started() === 0 )
				{
					if ( $this->auth()->user()->id() === $game->creator()->id() )
					{
						// The user is the game operator.
						if ( $player->user()->id() === $this->auth()->user()->id() )
						{
							// Remove game if the OP wants to leave.
							$this->markup( ' <a href="index.php?c=lobby&amp;a=' . LobbyController::GAME_DETAILS . '&amp;remove&amp;game=' . $game->id() . '&amp;player=' . $player->id() . '" class="button">Delete Game</a>' );
						}
						else
						{
							// Else add option to remove a player from the game.
							$this->markup( ' <a href="index.php?c=lobby&amp;a=' . LobbyController::GAME_DETAILS . '&amp;remove&amp;game=' . $game->id() . '&amp;player=' . $player->id() . '" class="button">Remove Player</a>' );
						}
					}
					elseif ( $this->auth()->user()->id() === $player->user()->id() )
					{
						// Leave game if you are the player, but not OP.
						$this->markup( ' <a href="index.php?c=lobby&amp;a=' . LobbyController::GAME_DETAILS . '&amp;leave&amp;game=' . $game->id() . '&amp;player=' . $player->id() . '" class="button">Leave Game</a>' );
					}
				}

				$this->markup( '</li>' . "\r\n" );
			}

			$this->markup( '                </ul>' . "\r\n"  );
			$this->markup( '              </dd>' . "\r\n"  );
			$this->markup( '              <dt>Game Options</dt>' . "\r\n" );
			$this->markup( '              <dd>' );

			if ( $game->maxPlayers() === count( $game->players() ) && $game->creator()->id() === $this->auth()->user()->id() && $game->started() === 0 )
			{
				// Add a start game button if the game is full and the user is the game operator.
				$this->markup( '<a href="index.php?c=lobby&amp;a=' . LobbyController::GAME_DETAILS . '&amp;start&amp;game=' . $game->id() . '" class="button">Start Game</a>' . "\r\n" );
			}

			if ( $game->started() === 1 )
			{
				// Add an open game button if the game is started.
				// index.php?c=lobby&amp;a=' . LobbyController::GAME_DETAILS . '&amp;start&amp;game=' . $game->id() . '
				$this->markup( '<a href="index.php?c=gameboard&amp;game=' . $game->id() . '" class="button">Open Gameboard</a>' . "\r\n" );
			}

			if ( !$playerExists && $game->started() === 0 )
			{
				// Add join button if the user is not already a participant.
				$this->markup( '<a href="index.php?c=lobby&amp;a=' . LobbyController::GAME_DETAILS . '&amp;join&amp;game=' . $game->id() . '" class="button">Join Game</a>' . "\r\n" );
			}

			$this->markup( '</dd>' . "\r\n" );
			$this->markup( '            </dl>' . "\r\n"  );
		}

		$this->markup( '          </div>' . "\r\n" );
	}

	private function buildGamesTable( $games )
	{
		$this->markup( '          <div class="lobby-section">' . "\r\n" );
		$this->markup( '            <table>' . "\r\n" );
		$this->markup( '              <thead>' . "\r\n" );
		$this->markup( '                <tr>' . "\r\n" );
		$this->markup( '                  <th>ID</th>' . "\r\n" );
		$this->markup( '                  <th>Creator</th>' . "\r\n" );
		$this->markup( '                  <th>Slots</th>' . "\r\n" );
		$this->markup( '                  <th>Players</th>' . "\r\n" );
		$this->markup( '                  <th>Map</th>' . "\r\n" );
		$this->markup( '                  <th>Enter</th>' . "\r\n" );
		$this->markup( '                </tr>' . "\r\n" );
		$this->markup( '              </thead>' . "\r\n" );
		$this->markup( '              <tbody>' . "\r\n" );
		foreach ( $games as $game )
		{
			$this->markup( '              <tr');
			if ( $game->finished() )
			{
				$this->markup( ' class="finished"' . "\r\n" );
			}
			elseif ( $game->started() )
			{
				$this->markup( ' class="started"' . "\r\n" );
			}
			else
			{
				$this->markup( ' class="open"' . "\r\n" );
			}
			$this->markup( '>' . "\r\n" );
			$this->markup( '                <td>' . $game->id() . '</td>' . "\r\n" );
			$this->markup( '                <td>' . $game->creator()->name() . '</td>' . "\r\n" );
			$this->markup( '                <td>' . $game->maxPlayers() . '</td>' . "\r\n" );
			$this->markup( '                <td>' . count( $game->players() ) . '</td>' . "\r\n" );
			$this->markup( '                <td>' . $game->map()->name() . '</td>' . "\r\n" );

			if ( $this->auth()->isLoggedIn() )
			{	
				$this->markup( '                <td><a href="index.php?c=lobby&amp;a=' . LobbyController::GAME_DETAILS . '&amp;game=' . $game->id() . '" class="button">Enter</a></td>' . "\r\n" );
			}

			else
			{
				$this->markup( '                <td><p>Login to see details</p></td>' . "\r\n" );
			}

			$this->markup( '              </tr>' . "\r\n" );
		}

		$this->markup( '              </tbody>' . "\r\n" );
		$this->markup( '            </table>' . "\r\n" );
		$this->markup( '          </div>' . "\r\n" );
	}

	private function buildCreateGame( $maps )
	{
		$this->markup( '          <div class="lobby-section">' . "\r\n" );
		$this->markup( '            <form action="index.php?c=lobby&amp;a=' . LobbyController::CREATE_GAME . '" method="POST">' . "\r\n" );
		$this->markup( '              <fieldset>' . "\r\n" );
		$this->markup( '                <h3>Create new game</h3>' . "\r\n" );
		
		$this->markup( '                <label for="create-max-players">Max Players:</label>' . "\r\n" );
		$this->markup( '                <select name="create-max-players" id="create-max-players">' . "\r\n" );
		for ( $i = 2; $i <= 8; $i++ )
		{
			$this->markup( '                  <option value="' . $i . '">' . $i . '</option>' . "\r\n" );
		}
		$this->markup( '                </select>' . "\r\n" );

		$this->markup( '                <label for="create-map-id">Map:</label>' . "\r\n" );
		$this->markup( '                <select name="create-map-id" id="create-map-id">' . "\r\n" );
		foreach ( $maps as $map )
		{
			$this->markup( '                  <option value="' . $map->ID() . '">' . $map->Name() . '</option>' . "\r\n" );
		}
		$this->markup( '                </select>' . "\r\n" );
		
		$this->markup( '                <input type="submit" name="create-submit" id="create-submit" value="Create Game" />' . "\r\n" );
		$this->markup( '              </fieldset>' . "\r\n" );
		$this->markup( '            </form>' . "\r\n" );
		$this->markup( '          </div>' . "\r\n" );
	}

	public function triedToCreateGame()
	{
		return isset( $_POST['create-submit'] );
	}

	public function getCreateMaxPlayers()
	{
		return isset( $_POST['create-max-players'] ) ? $_POST['create-max-players'] : null;
	}

	public function getCreateMapId()
	{
		return isset( $_POST['create-map-id'] ) ? $_POST['create-map-id'] : null;
	}

	public function gameId()
	{
		return isset( $_GET['game'] ) && preg_match( '/^[0-9]+$/', $_GET['game'] ) ? $_GET['game'] : null;
	}

	public function playerId()
	{
		return isset( $_GET['player'] ) && preg_match( '/^[0-9]+$/', $_GET['player'] ) ? $_GET['player'] : null;
	}

	public function triedToStartGame() {
		return isset( $_GET['start'] );
	}

	public function triedToJoinGame() {
		return isset( $_GET['join'] );
	}

	public function triedToLeaveGame() {
		return isset( $_GET['leave'] );
	}

	public function triedToRemovePlayer() {
		return isset( $_GET['remove'] );
	}
}
